import axios from 'axios';

const BASE_URL = 'http://localhost:3000/api';

export const getProducts = async (category = '', page = 1, sort = '') => {
  try {
    const response = await axios.get(`${BASE_URL}/products`, {
      params: {
        category,
        page,
        sort
      }
    });
    return response.data;
  } catch (error) {
    console.error('获取商品列表失败:', error);
    return [];
  }
};

export const searchProducts = async (keyword) => {
  try {
    const response = await axios.get(`${BASE_URL}/products/search`, {
      params: {
        keyword
      }
    });
    return response.data;
  } catch (error) {
    console.error('搜索商品失败:', error);
    return [];
  }
};

export const getCategories = async () => {
  try {
    const response = await axios.get(`${BASE_URL}/categories`);
    return response.data;
  } catch (error) {
    console.error('获取分类失败:', error);
    return [];
  }
};